<template>
  <ConfigDataList
    :cate="cate"
    :cols="cols"
  />
</template>
<script>
import ConfigDataList from "@/components/ConfigDataList";
import { getUser } from "@/utils/auth";
import { findComponent } from "@/utils/ui";
import { uuid } from "vue-uuid";
import linq from "linq";
import { search教师基本情况, get教师基本情况ByGH } from "@/api/教师基本情况";
import { add任务, delete任务, update任务, get任务 } from "@/api/任务.js";
import { add权限, delete权限, update权限, get权限 } from "@/api/权限.js";
export default {
  components: {
    ConfigDataList
  },
  data() {
    return {
      cate: "A14-2任务发布",
      cols: [
        { label: "姓名", prop: "XM" },
        { label: "录入任务表", prop: "INPUT_TABLES" },
         { label: "审核任务表", prop: "AUDIT_TABLES" },
        { label: "任务描述", prop: "DESC",  },
        { label: "开始时间", prop: "BEGINTIME",type:'date'},
        { label: "结束时间", prop: "ENDTIME" ,type:'date'}
      ]
    };
  },
  methods: {
    async onDlgCreated(model) {
      //
      var user = getUser();
      var datas = await get权限(user.XXID, user.CJPC);
      //
      var xm = findComponent(this, "XM");
      var cc = datas.map(item => {
        return { label: item.XM, value: item.XM };
      });
      xm.$attrs.items = cc;
      //
      
      if(model.XM){
        var right = linq.from(datas).firstOrDefault(`$.XM=='${model.XM}'`);
            var input_tables = findComponent(this, "INPUT_TABLES");
          input_tables.$attrs.items = right.ROLE_INPUT.split(",").map(item => {
            return { label: item, value: item };
          });
          input_tables.$attrs.items.unshift({value:"所有表",label:"所有表"});
          var audit_tables = findComponent(this, "AUDIT_TABLES");
          audit_tables.$attrs.items = right.ROLE_AUDIT.split(",").map(item => {
            return { label: item, value: item };
          });
          audit_tables.$attrs.items.unshift({value:"所有表",label:"所有表"});
      }
        
      //
    },
    async fetchData() {
      var user = getUser();
      var datas = await get任务(user.XXID, user.CJPC);
      datas.forEach(item=>{
          item.INPUT_TABLES = item.INPUT_TABLES.split(",").filter(x=>x!=="");
          item.AUDIT_TABLES = item.AUDIT_TABLES.split(",").filter(x=>x!=="");
          
      })
      return datas;
    },
    async onChange(model, field, value) {
      //
      var user = getUser();
      var datas = await get权限(user.XXID, user.CJPC);
      var tasks = await get任务(user.XXID, user.CJPC);
      tasks = tasks.filter(e=>e.ID != model.ID);
      if(field == "INPUT_TABLES"){
        
        if(value == "所有表"){
            var right = linq.from(datas).firstOrDefault(`$.XM=='${model.XM}'`);
          model.INPUT_TABLES = right.ROLE_INPUT.split(",");   
        }
       
         var tables1 = linq.from(tasks).where(`$.XM=='${model.XM}'`).select("$.INPUT_TABLES").toArray();
         
         var items=[];
         
         tables1.forEach(e=>items=items.concat(e.split(",")));
         if(linq.from(model.INPUT_TABLES).intersect(items).toArray().length>0)
         {
           this.$alert("选取的表已经存在以前的任务中");
            model.INPUT_TABLES=[];
           return;
         }
      }
       if(field == "AUDIT_TABLES"){
         if(value == "所有表"){
            var right = linq.from(datas).firstOrDefault(`$.XM=='${model.XM}'`);
            model.AUDIT_TABLES = right.ROLE_AUDIT.split(",");
        }
         var tables1 = linq.from(tasks).where(`$.XM=='${model.XM}'`).selectMany("$.AUDIT_TABLES").toArray();
         
         if(linq.from(model.AUDIT_TABLES).intersect(tables1).toArray().length>1)
         {
           this.$alert("选取的表已经存在以前的任务中");
            model.AUDIT_TABLES=[];
           return;
         }
      }
      if (field == "XM") {
         var right = linq.from(datas).firstOrDefault(`$.XM=='${value}'`);
            var input_tables = findComponent(this, "INPUT_TABLES");
          input_tables.$attrs.items = right.ROLE_INPUT.split(",").filter(e=>e !=="").map(item => {
            return { label: item, value: item };
          });
            input_tables.$attrs.items.length>0?input_tables.$attrs.items.unshift({value:"所有表",label:"所有表"}):'';
          var audit_tables = findComponent(this, "AUDIT_TABLES");
          audit_tables.$attrs.items = right.ROLE_AUDIT.split(",").filter(e=>e !=="").map(item => {
            return { label: item, value: item };
          });
          
          audit_tables.$attrs.items.length>0?audit_tables.$attrs.items.unshift({value:"所有表",label:"所有表"}):'';
        
      }
      //
    },
   
    onadd(model) {
      //
      
      var user = getUser();
      model.XXID = user.XXID;
      model.CJPC = user.CJPC;
      model.ID = uuid.v1();
      model.INPUT_TABLES = model.INPUT_TABLES.join(",");
      model.AUDIT_TABLES = model.AUDIT_TABLES.join(",");
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        
        console.log(JSON.stringify(temp));
        var model = await add任务(temp);
        resolve(model);
      });
    },
    onupdate(model) {
      //
     var temp = JSON.parse(JSON.stringify(model));
      temp.INPUT_TABLES = temp.INPUT_TABLES.join(",");
      temp.AUDIT_TABLES = temp.AUDIT_TABLES.join(",");
     
      return new Promise(async (resolve, reject) => {
        try {
          var model = await update任务(temp);
          resolve(model);
        } catch (e) {
          reject(e);
        }
      });
      //
    },
    ondelete(model) {
      var temp = JSON.parse(JSON.stringify(model));
      return new Promise(async (resolve, reject) => {
        await delete任务(temp);
        resolve();
      });
    }
  }
};
</script>
<style>
</style>